/*
 * version v2.1
 * Copyright © 2016 http://www.12306test.com, All rights reserved
 */
(function ($) {
    $.fn.extend({
        "validate": function (options) {
            if (!isValid(options))
                return this;
            var opts = $.extend({
            	css:{
            		padding:5,
            		width:67,
            		height:67
            	}
            }, defaluts, options);
            return this.each(function () {
                var $this = $(this);
                $this.css({
                	position:'relative'
                });
                $this.attr('_padding', opts.css.padding);
                $this.attr('_width', opts.css.width);
                $this.attr('_height', opts.css.height);
                $this.html("<span style='position:absolute;right:6px;top:6px;'><a href=\"javascript:;\" style=\"font-family:微软雅黑;font-size:12px;color:#666666;\"></a></span><img src=\"javascript:;\" />");
                $this.find("a").click(function(){
                	$.fn.validate.verifyCode($this, opts);
                });
                $this.find("img").click(function(e) {
//                	var x = e.pageX;
//                	var y = e.pageY;
    				var offset = $(this).offset();
    				var x = (e.pageX - offset.left)-($(this).parent().find("b").width()/2);
    				var y = (e.pageY - offset.top)-($(this).parent().find("b").height()/2);
//    				alert("图标位置："+x+","+ y);
    				if(y<17){
    					return false;
    				}
    				$(this).parent().append("<b style='position:absolute;left:"+x+"px;top:"+y+"px;color:red;cursor:default;' onclick='$.fn.validate.del(this)'>"+opts.select+"</b>");
    			});
                $.fn.validate.verifyCode($this, opts);
            });

        }
    });
    function isValid(options) {
        return !options || (options && typeof options === "object") ? true : false;
    }

    $.fn.validate.del = function(o) {
        $(o).remove();
    }
    $.fn.validate.verifyCode = function ($this, opts) {
    	$this.find('img').removeAttr('offset');
    	$this.find('b').remove();
		var vcimgUrl = defaluts.api+"?publicKey="+defaluts.publicKey+"&padding="+$this.attr('_padding')+"&width="+$this.attr('_width')+"&height="+$this.attr('_height')+"&r=" + Math.random();
		$.ajax({
			type:"get",
			url:vcimgUrl,
			dataType:"json",
			success:function(res){
				console.info(res);
				var result;
				if(res.status == 1){
					result = res.data;
//					var arr =  result.space;
//					var sp = "";
//					for(var i=0;i<arr.length;i++){
//						sp = sp+ arr[i].xleft+","+ arr[i].xright+","+ arr[i].yup+","+ arr[i].ydown+";";
//					 }
//					alert(sp);
//					$('#test').text(sp);
					var img = $this.find('img');
					img.attr("src", result.verifyImg);
					img.attr("uuid", result.verifyCode);
					img.attr("style", result.verifyType);
					$this.css({"width":result.width+"px"});
					$this.find("span a").html("刷新");
					$this.append("<b style='display:none;'>"+opts.select+"</b>");
				}else{
					alert(res.msg);
				}
				
			}
		});
	}
    $.fn.validate.clear = function (id){
    	$("#"+id).find("div").remove();
    }
    $.fn.validate.reflush = function (id){
    	$("#"+id).find("span > a").click();
    }
    $.fn.validate.submit = function ($this, action){
		if($this.find("b:visible").length==0){
			$this.append("<div style=\"width: "+$this.find("img").width()+"px; height: "+$this.find("img").height()+"px; position: absolute; top: 0px; left: 0px; background-color: #666666; z-index: 5000; opacity: 0.7; display: none; background-position: initial initial; background-repeat: initial initial;color:#ffffff;line-height:"+$this.find("img").height()+"px;font-size:16px;text-align:center;\">请点击相应答案!</div>");
			$this.find("div").show();
			setTimeout("$.fn.validate.clear('"+$this.attr('id')+"')", 1000);
			return;
		}
		var offset = new Array();
		$this.find("b:visible").each(function(){
			var position = $(this).position();
			if(position.left != "" && position.top != ""){
//				alert((position.left)+","+(position.top));
				offset.push((position.left)+","+(position.top));
			}
		});
		$.ajax({
			type:"POST",
			data:{"offset":offset.join(","),"code":$this.find('img').attr('uuid'),"appId":"a3a5c1faff9e41c2b2447a52c5bd7ea0"},
			url:action+"?r=" + Math.random(),
			dataType:"json",
			success:function(res){
				console.info(res);
				var result;
				if(res.status == 1){
					$("#message").html('<font color="green">验证成功</font>');
					setTimeout($.fn.validate.reflush("wb_validate"), 5000);
				}else{
//					alert(res.msg);
					$("#message").html('<font color="red">验证失败:'+res.msg+'</font>');
					setTimeout($.fn.validate.reflush("wb_validate"), 5000);
				}
			}
		});
	}
})(window.jQuery);